package hn.cch.druid.redis;

import com.google.gson.Gson;
import hn.cch.druid.entity.User;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * @author CCH
 * @version 2018.09.18
 */

@Repository
public class UserRedis extends CacheRedis {

    //存
    public void setUser(String key, User user, Long timeout){
        Gson gson = new Gson();
        String value  = gson.toJson(user);
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.MINUTES);
    }
    //取
    public User getUser(String key){
        User user = null;
        String json = redisTemplate.opsForValue().get(key);
        if (!StringUtils.isEmpty(json)){
            Gson gson = new Gson();
            user = gson.fromJson(json, User.class);
        }
        return user;
    }

}
